#pragma once
#include "new_api_comm.h"

typedef enum
{
#if TIMER0_COMPILE_EN
    TIMER0 = 0x40002000,
#endif
#if TIMER1_COMPILE_EN
    TIMER1 = 0x40002400,
#endif
#if TIMER2_COMPILE_EN
    TIMER2 = 0x40002800,
#endif
#if TIMER3_COMPILE_EN
    TIMER3 = 0x40002C00,
#endif
} timer_ch_t; // 定时器通道

// typedef enum
// {
//     PWM_OUT_HIGH = 0x00,
//     PWM_OUT_LOW = 0x01,
// } pwm_level_t; // 默认输出电平

// typedef enum
// {
//     PWM_DIV_1 = 0,
//     PWM_DIV_4,
//     PWM_DIV_8,
//     PWM_DIV_16,
// } pwm_div_t; // 分频系数

typedef void (*timer_cb_t)(void *arg); // 定时器回调函数

// 定时器
#define timer_enable(timer) (*((PUINT8V)(timer)) |= RB_TMR_COUNT_EN)                                                   // timer_ch_t
#define timer_disable(timer) (*((PUINT8V)(timer)) &= ~RB_TMR_COUNT_EN)                                                 // timer_ch_t
// #define timer_pwm_enable(timer) (*((PUINT8V)(timer)) |= RB_TMR_OUT_EN)                                                 // timer_ch_t
// #define timer_pwm_disable(timer) (*((PUINT8V)(timer)) &= ~RB_TMR_OUT_EN)                                               // timer_ch_t
// #define timer_pwm_set_cycle(timer, CYCLE) (*((PUINT32V)(timer + 12)) = CYCLE)                                          // timer_ch_t
// #define timer_pwm_set_duty(timer, DUTY) (*((PUINT32V)(timer + 16)) = DUTY)                                             // timer_ch_t
#define timer_it_mode(timer, en, mask) ((en) ? (*((PUINT8V)(timer + 2)) |= mask) : (*((PUINT8V)(timer + 2)) &= ~mask)) // timer_ch_t
// #define timer_set_time(timer, t) (*((PUINT32V)(timer + 12)) = t)                                                       // timer_ch_t
// #define timer_clear_cnt(timer) (*((PUINT8V)(timer)) = RB_TMR_ALL_CLEAR)                                                // timer_ch_t
// #define timer_clear_all_set(timer) (*((PUINT8V)(timer)) &= ~RB_TMR_ALL_CLEAR)                                          // timer_ch_t

// void timer_pwm_init(timer_ch_t timer, pwm_level_t level, pwm_div_t div);
void timer_init(timer_ch_t timer, uint32_t tic_ms, timer_cb_t cb, void *arg);
// void TMR_PFIC_EnableIRQ(timer_ch_t timer);
// void TMR_PFIC_DisableIRQ(timer_ch_t timer);